﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Dragon.Prototype1.Infrastructure.Controls;

namespace Dragon.Prototype1.Infrastructure.FormBuilder
{
    

    public class FormRow:jQueryControl
    {
        private int columnNumber;
        public FormRow(int columnNumber)
        {
            if (columnNumber <= 0)
                throw new ArgumentException("the column number of the form should be bigger than 0");
            this.columnNumber = columnNumber;
            LabelTdCssClass = "dataLabel";
        }

        public string LabelTdCssClass
        {
            get;
            set;
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Tr;
            }
        }

        List<LabeledControl> labelAndControlCollection = new List<LabeledControl>();
        public List<LabeledControl> LabelAndControlCollection
        {
            get { return this.labelAndControlCollection; }
        }

        public LabeledControl AddControl(Label label, WebControl control, bool isRequired)
        {
            LabeledControl newLabeledControl = new LabeledControl(label, control);
            newLabeledControl.IsRequired = isRequired;
            labelAndControlCollection.Add(newLabeledControl);
            return newLabeledControl;
        }

        public LabeledControl AddControl(Label label, WebControl control)
        {
            return this.AddControl(label, control,false);
        }

        public override void RenderJavaScriptContents(HtmlTextWriter writer)
        {
            foreach (LabeledControl ctrl in labelAndControlCollection)
            {
                if (ctrl.Control is jQueryControl)
                {
                    ((jQueryControl)ctrl.Control).RenderJavaScriptContents(writer);
                }
            }
        }

        public override void RenderContents(HtmlTextWriter writer)
        {
            int realColumnNumber = columnNumber * 2;
            string labelTdWidth="25%";
            string inputTdWidth="75%";
            if (columnNumber == 2)
            {
                labelTdWidth = "12.5%";
                inputTdWidth = "37.5%";
            }
            

            foreach (LabeledControl ctrl in labelAndControlCollection)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, LabelTdCssClass);
                writer.AddAttribute(HtmlTextWriterAttribute.Width, labelTdWidth);
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                ctrl.Label.Render(writer);
                if (ctrl.IsRequired)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "requiredlabel");
                    writer.RenderBeginTag(HtmlTextWriterTag.Span);
                    writer.Write("*");
                    writer.RenderEndTag();
                }
                writer.RenderEndTag();

                if (ctrl.OccupyWholeRow || labelAndControlCollection.Count == 1)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, (realColumnNumber - 1).ToString());
                }
                else
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Width, inputTdWidth);
                }
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                if (ctrl.IsRequired&&(ctrl.Control is TextBox))
                {
                    ctrl.Control.CssClass += " required";
                }
                ctrl.Control.Render(writer);
                writer.RenderEndTag();
            }
        }
    }
}
